package cn.ghf.product.controller;

import cn.ghf.basic.util.AjaxResult;
import cn.ghf.basic.util.PageList;
import cn.ghf.pet.domain.Pet;
import cn.ghf.product.domain.Product;
import cn.ghf.product.query.ProductQuery;
import cn.ghf.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    private PageList<Product> getProducts(@RequestBody ProductQuery query){
        PageList<Product> pets = productService.getProducts(query);
        return pets;
    }

    /**
     * 上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            productService.onSale(ids);
            return AjaxResult.createAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg("系统错误");
        }
    }

    /**
     * 下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            productService.offSale(ids);
            return AjaxResult.createAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg("系统错误");
        }
    }

    /**
     * 获取服务信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product getById(@PathVariable("id") Long id){
        return productService.loadById(id);
    }
}
